<div>
  <el-breadcrumb separator-class="el-icon-arrow-right" class="navi">
    <el-breadcrumb-item :to="{ path: '/login' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    <el-breadcrumb-item>用户列表</el-breadcrumb-item>
  </el-breadcrumb>
  <el-row :gutter="20" class="adduser">
    <el-col :span="8"
      ><div class="grid-content bg-purple">
        <el-input
          placeholder="请输入要查询的关键字"
          v-model="query"
          class="input-with-select"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="searchKey"
          ></el-button>
        </el-input></div
    ></el-col>
    <el-col :span="8"
      ><div class="grid-content bg-purple">
        <template>
          <el-button @click="dialogFormVisible = true">添加用户</el-button>
        </template>
      </div></el-col
    >
  </el-row>
  <el-table :data="usersData" style="width: 100%">
    <el-table-column prop="username" label="姓名" width="180"></el-table-column>
    <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
    <el-table-column prop="mobile" label="电话"></el-table-column>
    <el-table-column prop="mg_state" label="用户状态">
      <template slot-scope="scope">
        <el-switch
          v-model="scope.row.mg_state"
          @change="stateChanged(scope.row)"
        >
        </el-switch>
      </template>
    </el-table-column>
    <el-table-column prop label="操作">
      <template slot-scope="scope">
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-edit"
          @click="showEdite(scope.row)"
        ></el-button>
        <el-button
          type="danger"
          plain
          size="mini"
          icon="el-icon-delete"
          @click="deleteUser(scope.row.id)"
        ></el-button>
        <el-button
          type="success"
          plain
          size="mini"
          icon="el-icon-check"
          @click="showAssignRoles(scope.row)"
          >分配角色</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    background
    layout="prev, pager, next"
    :total="total"
    class="pagination"
    :page-size="2"
    :current-page="currentPage"
    @current-change="currentChange"
  ></el-pagination>

  <!-- <el-button>添加用户</el-button> -->
  <el-dialog
    title="添加用户"
    :visible.sync="dialogFormVisible"
    :close-on-click-modal="true"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      label-width="100px"
      class="demo-form"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer footer">
      <el-button @click="cancelForm" :plain="true">取 消</el-button>
      <el-button type="primary" @click="addData" :plain="true">确 定</el-button>
    </div>
  </el-dialog>

  <!-- 编辑角色 -->
  <el-dialog title="编辑用户" :visible.sync="dialogEditeVisible">
    <el-form :model="editeForm">
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-tag type="info">{{ editeForm.roleName }}</el-tag>
      </el-form-item>
      <el-form-item label="邮箱" :label-width="formLabelWidth">
        <el-input v-model="editeForm.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="电话" :label-width="formLabelWidth">
        <el-input v-model="editeForm.mobile" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer footer">
      <el-button @click="cancleEdite">取 消</el-button>
      <el-button type="primary" @click="editeUserData">确 定</el-button>
    </div>
  </el-dialog>

  <!-- 分配角色 -->
  <el-dialog title="分配角色" :visible.sync="assignRolesVisible">
    <el-form :model="assignRolesForm">
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-tag type="primary">{{ assignRolesForm.username }}</el-tag>
      </el-form-item>
      <el-form-item label="角色列表" :label-width="formLabelWidth">
        <!-- assignRolesForm.rid通过这个用户的角色id来标识它对应拥有哪一些角色 -->
        <el-select v-model="assignRolesForm.rid" placeholder="请选择角色">
          <!-- v-model值就是当前的value值，但是显示出来的是label的值，
              所以显示的是该id值下对应的roleName-->
          <!-- assignRolesForm.rid对应的就是item.id -->
          <el-option
            v-for="item in assignRolesForm.rolesList"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer footer">
      <el-button @click="hideAssignRoles">取 消</el-button>
      <el-button type="primary" @click="addAssignRoles">确 定</el-button>
    </div>
  </el-dialog>
</div>
